﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PromoCodes_Models.Abstract;
using PromoCodes_Models.Entities;
using PromoCodes_GUI.Models;

namespace PromoCodes_GUI.Controllers
{
    public class PromotionController : Controller
    {
        private IRepository<Product> _products;
        private IRepository<Promotion> _promotions;
        public PromotionController(IRepository<Product> products, IRepository<Promotion> promotions)
        {
            _products = products;
            _promotions = promotions;

            //TEST DATA - Remove to start from scratch
            if (_products.Items.Count() < 1)
            {
                _products.Edit(new Product { ProductName = "Featured" });
                _products.Edit(new Product { ProductName = "Ribbon" });
            }
            if (_promotions.Items.Count() < 25)
            {
                for (int i = _promotions.Items.Count(); i < 25; i++)
                {
                    if (i % 3 == 0)
                        _promotions.Edit(new Promotion { DiscountPercentage = 25, DiscountType = DiscountType.ORDER, MinimumTotalOrder = 25 * i, Expiration = DateTime.Now, MaxUses = 3, PromotionCode = "PROMO" + i });
                    else if (i % 3 == 1)
                        _promotions.Edit(new Promotion { DiscountPercentage = 12, DiscountType = DiscountType.ORDER, MinimumTotalOrder = 0, Expiration = DateTime.Now.Add(new TimeSpan(1, 0, 0)), PromotionCode = "PROMO" + i });
                    else
                        _promotions.Edit(new Promotion { DiscountPercentage = 33, DiscountType = DiscountType.PRODUCT, ProductID = 1, Product = _products.Items.First(x => x.ProductID == 1), Expiration = DateTime.Now.Add(new TimeSpan(5, 5, 5, 5)), PromotionCode = "PROMO" + i });
                }
            }
        }

        public ActionResult Index(int page = 1)
        {
            ListViewModel<Promotion> model = new ListViewModel<Promotion>
            {
                Info = new PagingInfo
                {
                    CurrentPage = page,
                    ItemsPerPage = 20,
                    TotalItems = _promotions.Items.Count()
                },
                PageItems = _promotions.Items.OrderBy(x => x.Expiration).Skip(20 * (page - 1)).Take(20)
            };
            return View(model);
        }

        public ActionResult AddPromotion(int ID = -1)
        {

            Promotion promotion =_promotions.Items.FirstOrDefault(x => x.PromotionID == ID);
            if( promotion == null)
            {
                promotion = new Promotion { Expiration = DateTime.Now, DiscountType=DiscountType.ORDER };
            }
            return View(new PromotionViewModel { AllProducts = new SelectList(_products.Items,"ProductID", "ProductName"), Promotion = promotion, ProductID=((promotion.ProductID != null)?(int)promotion.ProductID:0) });
        }


        [HttpPost]
        public ActionResult AddPromotion(PromotionViewModel promotionModel)
        {
            if (ModelState.IsValid)
            {
                Promotion check = _promotions.Items.FirstOrDefault(x => x.PromotionCode == promotionModel.Promotion.PromotionCode);
                if (check != null && promotionModel.Promotion.PromotionID < 1)
                {
                    ModelState.AddModelError("", "Promotion Code already in use.");
                }
                else
                {
                    if (promotionModel.Promotion.DiscountType == DiscountType.PRODUCT)
                    {
                        Product chosen = _products.Items.FirstOrDefault(x => x.ProductID == promotionModel.ProductID);
                        if (chosen == null)
                        {
                            ModelState.AddModelError("", "That product is no longer available.");
                        }
                        else
                        {
                            promotionModel.Promotion.Product = chosen;
                            promotionModel.Promotion.ProductID = promotionModel.ProductID;
                            promotionModel.Promotion.MinimumTotalOrder = 0;
                        }
                    }
                    else
                    {
                        promotionModel.Promotion.Product = null;
                        promotionModel.Promotion.ProductID = null;
                    }
                    if(ModelState.IsValid)
                    {
                        _promotions.Edit(promotionModel.Promotion);
                        return RedirectToAction("Index", "Promotion");
                    }
                }
            }
            promotionModel.AllProducts = new SelectList(_products.Items,"ProductID", "ProductName");
            //promotionModel.ProductID = (int)promotionModel.Promotion.ProductID;
            return View(promotionModel);
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            Promotion promo = _promotions.Items.FirstOrDefault(x => x.PromotionID == id);
            if (promo != null)
            {
                _promotions.Delete(promo);
            }
            return RedirectToAction("Index");
        }

    }
}
